package com.lankystar.logistucs.spring.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.lankystar.logistucs.spring.entity.Cargo;
import com.lankystar.logistucs.spring.entity.CargoInfo;
import com.lankystar.logistucs.spring.entity.User;
import com.lankystar.logistucs.spring.service.AddressService;
import com.lankystar.logistucs.spring.service.CargoService;
import com.lankystar.logistucs.spring.service.CompanyService;
import com.syx.spring.utils.FileUtil;


@Controller
@RequestMapping(value="/cargo")
public class CargoController {

	@Autowired
	private CargoService cargoService;
	@Autowired
	private CompanyService companyService;
	@Autowired
	private AddressService addressService;
	
	@RequestMapping(value="toAddCargo.do")
	public String toAddCargo(){
		return "redirect:/cargo/queryCargoInfolist.do";
	}
	
	@RequestMapping(value="queryCargoInfolist.do")
	public String queryCargoInfolist(Model model ,HttpSession ses){
		User user = (User) ses.getAttribute("userSession");
		List<CargoInfo> cargoInfolist = cargoService.queryCargoInfolist(user.getUserId());
		List<Map<String, Object>> addresslist = addressService.queryAddresslistByuserId(user.getUserId());
		List<Map<String, Object>> fleetlist = companyService.queryCompanyFeetlist();
		List<Map<String, Object>> brokerlist = companyService.queryCompanyBrokerlist();
		model.addAttribute("cargoInfolist", cargoInfolist);
		model.addAttribute("fleetlist", fleetlist);
		model.addAttribute("brokerlist", brokerlist);
		model.addAttribute("addresslist", addresslist);
		return "/cargo/addCargo";
	}
	
	@RequestMapping(value="queryCargolist.do")
	public String queryCargolist(Model model ,HttpSession ses){
		User user = (User) ses.getAttribute("userSession");
		List<Map<String, Object>> cargolist = cargoService.queryCargolist(user.getUserId());
		model.addAttribute("cargolist", cargolist);
		return "/cargo/cargolist";
	}
	
	@RequestMapping(value="addCargoInfo.do")
	public String AddCargoInfo(CargoInfo record,HttpSession ses){
		User user = (User) ses.getAttribute("userSession");
		record.setUser(user);
		cargoService.addCargoInfo(record);
		return "redirect:/cargo/queryCargoInfolist.do";
	}
	
	@RequestMapping(value="deleteCargoInfo.do")
	public String deleteCargoInfo(CargoInfo record){
		cargoService.deleteCargoInfo(record.getInfoId());
		return "redirect:/cargo/queryCargoInfolist.do";
	}
	
	@RequestMapping(value="toUpdateCargoInfo.do")
	public String toUpdateCargoInfo(CargoInfo record){
		
		return "/cargo/updateCargoInfo";
	}
	
	@RequestMapping(value="addCargo.do")
	public String AddCargo(Cargo record,MultipartFile file,HttpSession ses,HttpServletRequest req,Integer fleetId,Integer brokerId,Integer companyId,Integer addressId){
		try {
			User user = (User) ses.getAttribute("userSession");
			if(file!=null){
				String uploadPdf = FileUtil.uploadMethod(file, req);
				record.setUploadPdf(uploadPdf);
			}
			cargoService.addCargo(record,user,fleetId,brokerId,companyId,addressId);
		} catch (Exception e) {
			
		}
		return "redirect:/cargo/queryCargolist.do";
	}
	
	@RequestMapping(value="deleteCargo.do")
	public String deleteCargo(int cargoId){
		try {
			cargoService.deleteCargo(cargoId);
		} catch (Exception e) {
		}
		return "redirect:/cargo/queryCargolist.do";
	}
	
	@RequestMapping(value="toUpdateCargo.do")
	public String toUpdateCargo(Model model,int cargoId){
		Map<String, Object> map = cargoService.queryCargoAndAddressBycargoId(cargoId);
		model.addAttribute("map", map);
		return "/cargo/updateCargo";
	}
	
	@RequestMapping(value="updateCargo.do")
	public String updateCargo(Cargo cargo){
		return "/cargo/updateCargo";
	}
}
